package com.situ.company.employee.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.employee.model.EmployeeModel;
import com.situ.company.employee.service.IEmployeeService;
import com.situ.company.employee.service.impl.EmployeeServiceImpl;
import com.situ.company.util.FmtRequest;
import com.situ.company.util.Upload;

@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet{


	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String action=req.getParameter("action");
		Object res="";
		switch (action) {
		case "add":
		case "reg":
			res=reg(req);
			break;
		case "login":
			res=login(req);
			break;
		case "page":
			res=page(req);
			break;
		case "resetPass":
			res=resetPass(req);
			break;
		case "get":
			res=get(req);
			break;
		case "upd":
			res=upd(req);
			break;
		case "del":
			res=del(req);
			break;
		case "updCode":
			res=updCode(req);
			break;
		case "setDept":
			res=setDept(req);
			break;
		case "delPic":
			res=delPic(req);
			break;
		case "list":
			res=list(req);
			break;
		case "logout":
			req.getSession().invalidate();
			resp.sendRedirect(req.getContextPath()+"/web/login.jsp");
			break;
			
			
			
		default:
			break;
		}
		FmtRequest.write(resp.getWriter(),res);
	}
	private Object list(HttpServletRequest req) {
		
		EmployeeModel model=FmtRequest.parseModel(req, EmployeeModel.class);
		return service.selectList(model);
	}
	private Object delPic(HttpServletRequest req) {
		EmployeeModel model=new EmployeeModel(req.getParameter("code"));
		EmployeeModel mdb = service.selectModel(model);
		String image=mdb.getImage();
		Upload.delFile(image);
		mdb.setImage("");
		return service.update(mdb);
	}
	private Object setDept(HttpServletRequest req) {
		EmployeeModel model=FmtRequest.parseModel(req, EmployeeModel.class);
		return service.updateDept(model);
	}
	private Object updCode(HttpServletRequest req) {
		EmployeeModel model=new EmployeeModel(req.getParameter("code"));
		return service.delete(model);
	}
	private Object del(HttpServletRequest req) {
		EmployeeModel model=new EmployeeModel(req.getParameter("code"));
		return service.delete(model);
	}
	private Object upd(HttpServletRequest req) {
		EmployeeModel model=FmtRequest.parseModel(req, EmployeeModel.class);
		return service.update(model);
	}
	private Object get(HttpServletRequest req) {
		EmployeeModel model=new EmployeeModel(req.getParameter("code"));
		return service.selectModel(model);
	}
	private Object resetPass(HttpServletRequest req) {
		EmployeeModel model=new EmployeeModel(req.getParameter("code"));
		
		return service.resetPass(model);
	}
	private Object page(HttpServletRequest req) {
		//Map必须写，请求中还包含pageLimit，pageIndex会被反射，找不到
		Map<String,String> prop=new HashMap<>();
		prop.put("name", "name");
		prop.put("code", "code");
		EmployeeModel model=FmtRequest.parseModel(req, EmployeeModel.class,prop);
		String pageIndex=req.getParameter("pageIndex");
		String pageLimit=req.getParameter("pageLimit");
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		List<EmployeeModel> list = service.selectList(model);
		Integer count=service.selectCount(model);
		Map<String,Object> map=new HashMap<>();
		map.put("list", list);
		map.put("count", count);
		return map;
	}
	private Object login(HttpServletRequest req) {
		EmployeeModel model=FmtRequest.parseModel(req, EmployeeModel.class);
		String res=service.login(model);
		if("1".equals(res))
			req.getSession().setAttribute("user",service.selectModel(model));
		return res;
	}
	private IEmployeeService service=new EmployeeServiceImpl();
	private String reg(HttpServletRequest req) {
//		String code=req.getParameter("code");
//		String pass=req.getParameter("pass");
//		String name=req.getParameter("name");
//		EmployeeModel model=new EmployeeModel(code,name,pass);
		EmployeeModel model=FmtRequest.parseModel(req, EmployeeModel.class);
		return service.insert(model);
	}
	

	
	
}
